﻿using System;
using System.Windows.Controls;
using System.Globalization;

namespace CCF20
{

    /// <summary>
    /// This class is based on the numeric only WPF Textbox implementation
    /// found at the following blog 
    /// 
    /// http://dotnetus.spaces.live.com/Blog/cns!4E39ECD492E4EEC1!550.entry
    /// 
    /// It provides a textbox that only allows letters to be entered
    /// </summary>
    public class LettersOnlyTextBox : TextBox
    {

        #region Overrides
        protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
        {
            e.Handled = !AreAllValidLetterChars(e.Text);
            base.OnPreviewTextInput(e);
        }
        #endregion

        #region Private Methods
        private bool AreAllValidLetterChars(string str)
        {
            bool ret = true;
            if (str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencyGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.CurrencySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeSign |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NegativeInfinitySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.NumberGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentDecimalSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentGroupSeparator |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PercentSymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PerMilleSymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveInfinitySymbol |
                str == System.Globalization.NumberFormatInfo.CurrentInfo.PositiveSign)
                return !ret;

            int l = str.Length;
            for (int i = 0; i < l; i++)
            {
                char ch = str[i];
                ret &= !Char.IsDigit(ch);
            }

            return ret;
        }
        #endregion
    }

}
